//
//  IFIndexFocusCell.m
//  chanlin
//
//  Created by Ryan on 14-11-30.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "IFIndexFocusCell.h"
#import "UIImageView+WebCache.h"
#import "UIView+Utils.h"


#define IFIndexFocusImgHeight  180.0f
#define IFIndexFocusImgWidth   320.0f

@interface IFIndexFocusCell ()
@property (nonatomic,strong)IfengCycleScrollView *cycleFocusView;//循环滚动焦点view
@property (nonatomic,strong)NSMutableArray *slideImgViewArray;//保存焦点图imageView
@property (nonatomic,strong)UIFont *titleFont;
@property (nonatomic,strong)UIFont *currentIndexLabelFont;

@end

@implementation IFIndexFocusCell

- (void)uiInit{
    [super uiInit];
    _cycleFocusView = [[IfengCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, IFIndexFocusImgWidth, IFIndexFocusImgHeight)];
    _cycleFocusView.delegate = self;
    _cycleFocusView.datasource = self;
    [self addSubview:_cycleFocusView];
    self.titleFont=[UIFont fontWithName:@"Menlo-Regular" size:8.f];
    self.currentIndexLabelFont=[UIFont fontWithName:@"Menlo-Regular" size:15.f];
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"focus_background.png"];
    imageView.frame =  CGRectMake(0, IFIndexFocusImgHeight-47.0f, IFIndexFocusImgWidth, 47.f);
   
    self.indexView = imageView;
    [self addSubview:imageView];
    [self bringSubviewToFront:imageView];
    
    CustomCoretextLabel *label = [[CustomCoretextLabel alloc] init];
    label.numberOfLines = 1;
    label.textAlignment = NSTextAlignmentRight;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont fontWithName:@"Menlo-Regular" size:8.f];
    label.textColor =RGB(0xb6, 0xb8, 0xbb);
    label.frame = CGRectMake(IFIndexFocusImgWidth-12.f-20.f,47.f-15.f-9.f, 20.f, 19.f);
   
    self.indexLabel = label;
    [_indexView addSubview:label];
    
    
    UILabel  *titleLabel = [[UILabel alloc] init];
    titleLabel.numberOfLines = 1;
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:14];
    titleLabel.textColor = [UIColor whiteColor];
   
    titleLabel.shadowOffset = CGSizeMake(0.5, 0.5);
    titleLabel.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.8f];
    titleLabel.frame = CGRectMake(cellEdgeCap,47.f-15.f-9.f, 270.f, 15.f);
    self.title = titleLabel;
    [_indexView addSubview:titleLabel];
    [self addGesture];
}



- (void)loadData:(CLList *)newsList{
    [self reloadCell:0 indexList:newsList];

}

- (void)loadTopicData:(CLTopic *)newsList{
    [self reloadCell2:0 indexList:newsList];
}





- (void)addGesture
{
    UITapGestureRecognizer *aRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
    aRec.numberOfTapsRequired = 1;
    aRec.numberOfTouchesRequired = 1;
    [self addGestureRecognizer:aRec];
    
}

- (void)didTap:(UITapGestureRecognizer *)aRec
{
    if(self._blkTapResponse)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        self._blkTapResponse(self.indexList,self.currentIndex,indexPath);
        
    }
    
    if(self._blkTapResponse2){
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        self._blkTapResponse2(self.topicList,self.currentIndex,indexPath);

    }
}

-(int)fitCountForCycleScrollView{//当条数为2时特殊处理
    return (2==[self photoCount])?4:[self photoCount];
}
-(int)fitIndexForCycleScrollView:(int)originalIndex{//当条数为2时特殊处理
    return (2==[self photoCount])?(originalIndex%2):originalIndex;
}

- (void)reloadCell:(int)acurrentIndex indexList:(CLList *)indexList{
    self.indexList = indexList;
    self.currentIndex = acurrentIndex;
    [self reload];
}
- (void)reloadCell2:(int)acurrentIndex indexList:(CLTopic *)indexList{
    self.topicList = indexList;
    self.currentIndex = acurrentIndex;
    [self reload];
}

-(void)reSetSlideImgViewArray{
    if (_slideImgViewArray) {
        int count = [self fitCountForCycleScrollView];
        int slideCount = [_slideImgViewArray count];
        for (int i=0; i<fabs(count-slideCount); i++) {
            if(count<slideCount) [_slideImgViewArray sgrRemoveForIndex:slideCount-1-i];
            else{
                UIImageView *newSlideImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, IFIndexFocusImgWidth, IFIndexFocusImgHeight)];
                newSlideImage.backgroundColor= [UIColor clearColor];
                [_slideImgViewArray  sgrAddObject:newSlideImage];
            }
        }
    }
  
}

- (void)loadDataAtIndex:(int)index
{
    //设置索引
    NSString *indexString = [NSString stringWithFormat:@"%d/%d",index+1,[self photoCount]];
   
    [self.indexLabel setText:indexString WithFont:self.titleFont AndColor:RGB(0xb6, 0xb8, 0xbb)];
    [self.indexLabel setTextdBold:[indexString rangeOfString:[NSString stringWithFormat:@"%d",index+1]]
                         WithFont:self.currentIndexLabelFont
                         AndColor:RGB(0xf5, 0x43, 0x43)];
    
    //设置标题
    NSString *title = self.topicList?[self subIndexListForIndex2:index].title:[self subIndexListForIndex:index].title;
    if(_isStrNULL(title)) self.title.text = @"";
    self.title.text = title;
    
}

- (CLList *)subIndexListForIndex:(int)index{
  
    CLList *subIndexList = [self.indexList.focus sgrOrderSetObjectAtIndex:index];
    if(subIndexList && [subIndexList isKindOfClass:[CLList class]]){
        return subIndexList;
    }
  
    
   
    return nil;
}

- (CLTopic *)subIndexListForIndex2:(int)index{
    CLTopic *subIndexList = [self.topicList.focus sgrOrderSetObjectAtIndex:index];
    if(subIndexList && [subIndexList isKindOfClass:[CLTopic class]]){
        return subIndexList;
    }
    return nil;
}

- (int)photoCount{
    if(self.topicList){
        return [self.topicList.focus count];
    }else{
    return [self.indexList.focus count];
    }
}

- (void)reload{
    
    [self reSetSlideImgViewArray];
    [self.cycleFocusView reloadData];
}

#pragma -
#pragma mark -CycleScrollViewDelegate,CycleScrollViewDatasource

- (NSInteger)cSGetNumberOfPages
{
    if (!_slideImgViewArray) {
        int count = [self fitCountForCycleScrollView];
        _slideImgViewArray = [[NSMutableArray alloc] initWithCapacity:count];
        for (int i=0; i<count; i++) {
            UIImageView *slideImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, IFIndexFocusImgWidth, IFIndexFocusImgHeight)];
            [_slideImgViewArray sgrAddObject:slideImage];
            slideImage.backgroundColor= [UIColor clearColor];
            
        }
    }
    
    return [_slideImgViewArray count];
}

- (UIView *)cSGetPageViewAtIndex:(NSInteger)index
{
    int _index = [self fitIndexForCycleScrollView:index];
    UIImageView *slideImageV=[_slideImgViewArray sgrGetType:[UIImageView class] forIndex:index];
    
    NSString *str=self.topicList?[self subIndexListForIndex2:_index ].thumnail: [self subIndexListForIndex:_index ].thumnail;
    if(_isStrNotNull(str)){
    [slideImageV sd_setImageWithURL:[NSURL URLWithString:str]
                   placeholderImage:[UIImage imageNamed:@"index_focus_default.png"]];
    }else{
        slideImageV.image=[UIImage imageNamed:@"index_focus_default.png"];
    }
    
    UIView *IconView = [slideImageV viewWithTag:333];
    if(!IconView){
        UIImageView *playIcon=[[UIImageView alloc] init];
        playIcon.image=[UIImage imageNamed:@"doc_playImage.png"];
        playIcon.tag=333;
        playIcon.frame=CGRectMake((slideImageV.width-48.0f)/2.0f,
                                  (slideImageV.height-48.0f)/2.0f, 48.0f, 48.0f);
        
        IconView = playIcon;
        [slideImageV addSubview:playIcon];
    }
    if(self.topicList){
         IconView.hidden = !(((CLTopic *)[self.topicList.focus sgrOrderSetObjectAtIndex:index%[self.topicList.focus count]]).link==CLList_video);
    }else{
         IconView.hidden = !(((CLList *)[self.indexList.focus sgrOrderSetObjectAtIndex:index%[self.indexList.focus count]]).link==CLList_video);
    }
   
//
//    slideImageV.frame=CGRectMake(0, 0, IFIndexFocusImgWidth, IFIndexFocusImgHeight);
//    NSString *thumbnail=[self subIndexListForIndex:_index ].imgUrl;
//    [slideImageV reset2DefaultWithUrl:thumbnail];
//    if(![slideImageV checkIsNoChange:thumbnail]){
//        [slideImageV setPlaceholderImage:ICurrentTheme.focusImageCellStyle.defaultImage];
//        
//    }
//    [slideImageV setUrl4Image:thumbnail];
    return slideImageV;
}

- (void)cSLoadDataAtIndex:(int)index
{
    int _index = [self fitIndexForCycleScrollView:index];
    self.currentIndex=_index;
    
    //设置索引
    NSString *indexString = [NSString stringWithFormat:@"%d/%d",_index+1,[self photoCount]];
    [self.indexLabel setText:indexString WithFont:self.titleFont AndColor:RGB(0xb6, 0xb8, 0xbb)];
    [self.indexLabel setTextdBold:[indexString rangeOfString:[NSString stringWithFormat:@"%d",_index+1]]
                         WithFont:self.currentIndexLabelFont
                         AndColor:RGB(0xf5, 0x43, 0x43)];
    
    //设置标题
    NSString *title = [self subIndexListForIndex:_index].title;
    if(_isStrNULL(title)) self.title.text = @"";
    self.title.text = title;
}

@end
